<?php
include('./include/conn.php');
if(!isset($_SESSION['u'])){
	echo "<script>alert('查看话题内容，请登录！');window.location.href='login.php';</script>";
}

if(!isset($_GET['id'])){
	echo "<script>alert('非法操作！');window.location.href='index.php';</script>";
}

$query = "SELECT * FROM `msg` WHERE `id`='".$_GET['id']."'";
$rs = $link->query($query);
$row = $rs->fetch_assoc();
//var_dump($row);
?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>讨论组BBS</title>
	<link rel="stylesheet" type="text/css" href="./include/style.css">
	<script type="text/javascript">
		function check_reply(){
			if(reply_form.msg.value.lenght==0){
				alert('回复不能为空！');
				return reply_form.msg.focus();
			}else if(reply_form.msg.value.lenght > 60){
				alert('评论字数超过了60');
				return reply_form.msg.focus();
			}else{
				reply_form.submit();
			}
		}
	</script>
</head>
<body>
	<div class="container">
		<header>
			<a href="index.php"><h1>讨论组BBS</h1></a>
		</header>
        <nav>
        	<div class="search">
            	<form method="post" action="search.php">
                	<input type="text" class="w" name="w"><input type="submit" class="seach_btn" value="搜索">
                </form>
            </div>
            <ul>
            <?php if(isset($_SESSION['u'])){
				echo "<li id='userid'>".strtoupper($_SESSION['u'])."</li>";
				echo "<li><a href='logout.php'>登出</a><li>"	;
			}
			?>
            </ul>
        </nav>
        <article>
        	<div class="desc_content">
            	<div class="title"><?php echo $row['title']; ?></div>
                <div class="author_time"><?php echo $row['author']; ?><span><?php echo $row['time']; ?></span></div>
                <div class="content"><p><?php echo $row['content']; ?></p></div>
                <div class="img"><?php if(!empty($row['islike'])) echo $row['islike']; ?>&nbsp;<a href="like.php?id=<?php echo $row['id'];?>"><img src="./img/good.png" 
                id="like"></a>&nbsp;赞&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<?php if(!empty($row['deslike'])) echo $row['deslike']; ?>&nbsp;<a href="deslike.
                php?id=<?php echo $row['id'];?>"><img src="./img/good.png" id="deslike"></a>&nbsp;踩</div>
            </div>
            <!--清除浮动-->
            <div id="clear"></div>
            <!--对应当前话题评论-->
            <?php
            		$query = "SELECT * FROM `content` WHERE `id`='".$_GET['id']."'";
            		
            		$rs = $link->query($query);
            		if($rs -> num_rows != 0){
                        while($reply_row = $rs->fetch_assoc()) {
                            echo "<div class='reply_content'><span>".strtoupper($reply_row['author'])."</span>".$reply_row['content']."</div>";
                        }
                    }else{
                        echo "<div class='reply_content'><p>暂无评论</p></div>";
                    }
            ?>
            <!--写评论-->
            <form method="post" name="reply_form" action="reply_ok.php">
            <input type="hidden" name="mid" value="<?php echo $_GET['id']; ?>">
            <input type="hidden" name="author" value="<?php echo $_SESSION['u']; ?>">
            <div class="reply_msg">
            	<textarea name="msg" placeholder="评论字数为60个字符！"></textarea><input type="button" id="btn" value="评论" onclick="check_reply()">
            </div>
            </form>
        </article>
	</div>
</body>
</html>